package drone.iqgc.service ;

import java.util.List ;
import java.util.Objects ;
import java.util.Map.Entry ;

import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;

import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONObject ;

import drone.basis.util.NumberUtil ;
import drone.iqgc.common.DroneManager ;
import drone.iqgc.common.Message ;
import drone.iqgc.common.WebsocketServer ;
import io.mavsdk.System ;
import io.mavsdk.mission.Mission.MissionItem ;
import io.mavsdk.mission.Mission.MissionPlan ;

@Service
public class MessageService
{

	@Autowired
	private DroneManager droneManager ;

	public void droneStatusMessage ( )
	{
		for ( Entry < String , System > e : droneManager.getDrones ( ).entrySet ( ) )
		{
			String message = Message.generate ( "1" , "" , "msg" , "status" , e.getKey ( ) , JSONObject.parseObject ( "{'status':'1'}" ) ) ;
			WebsocketServer.message ( message ) ;
		}
	}

	public void droneStatusMessage (
			String code ,
			String msg ,
			String id ,
			String status
	)
	{
		String message = Message.generate ( code , msg , "msg" , "status" , id , JSONObject.parseObject ( "{'status':'" + status + "'}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void dronePositionMessage (
			String code ,
			String msg ,
			String id ,
			Double lng ,
			Double lat ,
			Float abs ,
			Float rel
	)
	{

		JSONObject data = new JSONObject ( ) ;

		data.put ( "lng" , NumberUtil.transfer ( lng , 4 ) ) ;
		data.put ( "lat" , NumberUtil.transfer ( lat , 4 ) ) ;
		data.put ( "abs" , NumberUtil.transfer ( abs , 2 ) ) ;
		data.put ( "rel" , NumberUtil.transfer ( rel , 2 ) ) ;
		
		String message = Message.generate ( code , msg , "msg" , "position" , id , data ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneFlightmodeMessage (
			String code ,
			String msg ,
			String id ,
			String flightmode
	)
	{
		String message = Message.generate ( code , msg , "msg" , "flightmode" , id , JSONObject.parseObject ( "{'flightmode':'" + flightmode + "'}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneMaxspeedMessage (
			String code ,
			String msg ,
			String id ,
			Float maxspeed
	)
	{

		String message = Message.generate ( code , msg , "msg" , "maxspeed" , id , JSONObject.parseObject ( "{'maxspeed':'" + maxspeed + "'}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneHomeMessage (
			String code ,
			String msg ,
			String id ,
			Double lng ,
			Double lat ,
			Float abs ,
			Float rel
	)
	{

		JSONObject data = new JSONObject ( ) ;

		data.put ( "lng" , NumberUtil.transfer ( lng , 4 ) ) ;
		data.put ( "lat" , NumberUtil.transfer ( lat , 4 ) ) ;
		data.put ( "abs" , NumberUtil.transfer ( abs , 2 ) ) ;
		data.put ( "rel" , NumberUtil.transfer ( rel , 2 ) ) ;

		String message = Message.generate ( code , msg , "msg" , "home" , id , data ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneGotoMessage (
			String code ,
			String msg ,
			String id
	)
	{
		String message = Message.generate ( code , msg , "msg" , "goto" , id , JSONObject.parseObject ( "{}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneTakeoffMessage (
			String code ,
			String msg ,
			String id
	)
	{
		String message = Message.generate ( code , msg , "msg" , "takeoff" , id , JSONObject.parseObject ( "{}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneLandMessage (
			String code ,
			String msg ,
			String id
	)
	{
		String message = Message.generate ( code , msg , "msg" , "land" , id , JSONObject.parseObject ( "{}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneReturnMessage (
			String code ,
			String msg ,
			String id
	)
	{
		String message = Message.generate ( code , msg , "msg" , "return" , id , JSONObject.parseObject ( "{}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneHomeMessage (
			String code ,
			String msg ,
			String id
	)
	{
		String message = Message.generate ( code , msg , "msg" , "home" , id , JSONObject.parseObject ( "{}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneAltitudeMessage (
			String code ,
			String msg ,
			String id
	)
	{
		String message = Message.generate ( code , msg , "msg" , "altitude" , id , JSONObject.parseObject ( "{}" ) ) ;
		WebsocketServer.message ( message ) ;
	}
	
	public void droneCameraMessage (
			String code ,
			String msg ,
			String id
	)
	{
		String message = Message.generate ( code , msg , "msg" , "camera" , id , JSONObject.parseObject ( "{}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneMissionUploadMessage (
			String code ,
			String msg ,
			String id ,
			String percent
	)
	{
		String message = Message.generate ( code , msg , "msg" , "missionUpload" , id , JSONObject.parseObject ( "{'percent':'" + percent + "'}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneMissionDownloadMessage (
			String code ,
			String msg ,
			String id ,
			MissionPlan plan
	)
	{

		if ( Objects.isNull ( plan ) )
		{
			String message = Message.generate ( code , msg , "msg" , "missionDownload" , id , new JSONArray ( ) ) ;
			WebsocketServer.message ( message ) ;
			return ;
		}

		List < MissionItem > items = plan.getMissionItems ( ) ;

		JSONArray array = new JSONArray ( ) ;
		for ( MissionItem item : items )
		{
			JSONObject o = new JSONObject ( ) ;
			o.put ( "latitudeDeg" , item.getLatitudeDeg ( ) ) ;
			o.put ( "longitudeDeg" , item.getLongitudeDeg ( ) ) ;
			o.put ( "relativeAltitudeM" , item.getRelativeAltitudeM ( ) ) ;
			o.put ( "speedMS" , item.getSpeedMS ( ) ) ;
			o.put ( "isFlyThrough" , item.getIsFlyThrough ( ) ) ;
			o.put ( "gimbalPitchDeg" , item.getGimbalPitchDeg ( ) ) ;
			o.put ( "gimbalYawDeg" , item.getGimbalYawDeg ( ) ) ;
			o.put ( "cameraAction" , item.getCameraAction ( ) ) ;
			o.put ( "loiterTimeS" , item.getLoiterTimeS ( ) ) ;
			o.put ( "cameraPhotoIntervalS" , item.getCameraPhotoIntervalS ( ) ) ;
			o.put ( "acceptanceRadiusM" , item.getAcceptanceRadiusM ( ) ) ;
			o.put ( "yawDeg" , item.getYawDeg ( ) ) ;
			o.put ( "cameraPhotoDistanceM" , item.getCameraPhotoDistanceM ( ) ) ;
			array.add ( o ) ;
		}

		String message = Message.generate ( code , msg , "msg" , "missionDownload" , id , array ) ;

		WebsocketServer.message ( message ) ;
	}

	public void droneMissionStartMessage (
			String code ,
			String msg ,
			String id ,
			String percent
	)
	{
		String message = Message.generate ( code , msg , "msg" , "missionStart" , id , JSONObject.parseObject ( "{'percent':'" + percent + "'}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneMissionCanelMessage (
			String code ,
			String msg ,
			String id
	)
	{
		String message = Message.generate ( code , msg , "msg" , "missionCanel" , id , JSONObject.parseObject ( "{}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void dronHealthMessage (
			String code ,
			String msg ,
			String id ,
			String homeposition ,
			String armable
	)
	{
		String message = Message.generate ( code , msg , "msg" , "health" , id , JSONObject.parseObject ( "{'homeposition':'" + homeposition + "','armable':'" + armable + "'}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneBatteryMessage (
			String code ,
			String msg ,
			String id ,
			String battery
	)
	{
		String message = Message.generate ( code , msg , "msg" , "battery" , id , JSONObject.parseObject ( "{'battery':'" + battery + "'}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneLandedStateMessage (
			String code ,
			String msg ,
			String id ,
			String landedstate
	)
	{
		String message = Message.generate ( code , msg , "msg" , "landedstate" , id , JSONObject.parseObject ( "{'landedstate':'" + landedstate + "'}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

	public void droneVtolStateMessage (
			String code ,
			String msg ,
			String id ,
			String vtolstate
	)
	{
		String message = Message.generate ( code , msg , "msg" , "vtolstate" , id , JSONObject.parseObject ( "{'vtolstate':'" + vtolstate + "'}" ) ) ;
		WebsocketServer.message ( message ) ;
	}

}
